package com.maoslv.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class JWTUtil {

    private static final String SIGN = "!Q@W%E$S";

    public static String getToken(String username) {

        // 过期时间 7天后过期
        long time = new Date().getTime() + 1 * 1000 * 3600 * 24 * 7;

        String token = JWT.create()
                .withClaim("username",username)
                .withExpiresAt(new Date(time)) // 设置过期时间
                .sign(Algorithm.HMAC256(SIGN)); // 签名 密钥
        return token;
    }

    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }
}
